package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.system.entity.Region;
import cn.signalpha.modules.system.service.IRegionService;
import cn.signalpha.modules.system.vo.RegionVO;
import cn.signalpha.modules.system.wrapper.RegionWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * 控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/region")
@Api(value = "行政区划", tags = "行政区划")
public class SpiRegionController extends BaseController {

	private final IRegionService regionService;

	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "区划编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "parentCode", value = "父区划编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "区划名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "status", value = "状态：0隐藏 1显示", paramType = "query", dataType = "string"),
	})
	@ApiOperation(value = "列表", notes = "传入code")
	public R<List<Region>> list(@ApiIgnore @RequestParam Map<String, Object> param) {
		List<Region> list = regionService.list(Wrappers.<Region>query().lambda()
			.eq(ObjectUtil.isNotEmpty(param.get("parentCode")), Region::getParentCode, param.get("parentCode"))
			.eq(ObjectUtil.isNotEmpty(param.get("code")), Region::getCode, param.get("code"))
			.eq(ObjectUtil.isNotEmpty(param.get("status")), Region::getStatus, param.get("status"))
			.like(ObjectUtil.isNotEmpty(param.get("name")), Region::getName, param.get("name"))
		);
		return R.data(list);
	}

	@GetMapping("/lazy-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "区划编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "区划名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "status", value = "状态：0隐藏 1显示", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "懒加载列表", notes = "传入menu")
	public R<List<RegionVO>> lazyList(@RequestParam(defaultValue = "00") String parentCode, @ApiIgnore @RequestParam Map<String, Object> param) {
		List<RegionVO> list = regionService.lazyList(parentCode, param);
		return R.data(RegionWrapper.build().listNodeLazyVO(list));
	}

	@GetMapping("/tree")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "status", value = "状态：0隐藏 1显示", paramType = "query", dataType = "string"),
	})
	@ApiOperation(value = "树形结构", notes = "传入menu")
	public R<List<RegionVO>> lazyTree(@ApiIgnore @RequestParam Map<String, Object> param) {
		List<RegionVO> list = regionService.tree(param);
		return R.data(RegionWrapper.build().listNodeLazyVO(list));
	}

	@GetMapping("/city")
	@ApiOperation(value = "获取所有市", notes = "")
	public R<List<Region>> city() {
		return R.data(regionService.list(Wrappers.<Region>lambdaQuery().eq(Region::getRegionLevel, 2)));
	}

}
